//
//  searchViewController.m
//  mintHome
//
//  Created by 朱彦名 on 16/6/29.
//  Copyright © 2016年 朱彦名. All rights reserved.
//

#import "searchViewController.h"
#import "cellOfFirst.h"
#import "modelOfMintHome.h"

@interface searchViewController ()<UISearchBarDelegate, UISearchResultsUpdating, UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) UISearchController *search;
@property (nonatomic, retain) NSString *searchStr;
@property (nonatomic, retain) NSMutableArray *dataArr;
@property (nonatomic, retain) UITableView *tabelView1;



@end

@implementation searchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)creatView{
    
    UIBarButtonItem *backButon = [[[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(back)]autorelease];
    self.navigationItem.leftBarButtonItem = backButon;
    
    // 初始化搜索视图
    self.search = [[UISearchController alloc] initWithSearchResultsController:nil];
    //签协议
    _search.searchResultsUpdater = self;
    //屏幕变灰 yes时tabView 不能下拉
    _search.dimsBackgroundDuringPresentation = NO;
    //设置点击搜索框时 隐藏导航栏
    _search.hidesNavigationBarDuringPresentation = NO;
    //占位符 默认search
    _search.searchBar.placeholder = @"搜索感兴趣的内容";
    
    //搜索栏的风格
    _search.searchBar.searchBarStyle = UISearchBarStyleMinimal;
    
    //把搜索视图加到导航栏上
    self.navigationItem.titleView = _search.searchBar;

    
    self.tabelView1 = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    

    self.tabelView1.delegate = self;
    self.tabelView1.dataSource = self;
    
    
    
    [self.tabelView1 registerClass:[cellOfFirst class] forCellReuseIdentifier:@"pool"];
    
    [self.view addSubview:self.tabelView1];
    

}
- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_dataArr.count != 0) {
        
        cellOfFirst *cell1 = [tableView dequeueReusableCellWithIdentifier:@"pool" forIndexPath:indexPath];
        
        modelOfMintHome *model = _dataArr[indexPath.row];
        
        
        cell1.labelOfTitle.text = model.title;
        
        [cell1.imageOfMint sd_setImageWithURL:[NSURL URLWithString:model.cover_image_url]];
        
        
        

        
        return cell1;
    }else{
    
        return 0;
    }
    
    

}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{

    self.searchStr = self.search.searchBar.text;
    //
    NSString *strUrl = [_searchStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:_searchStr]];

    NSString *newStr = [NSString stringWithFormat:@"http://api.bohejiaju.com/v1/search/post?keyword=%@&limit=20&offset=0&sort=", strUrl ];
    
    
    
    self.dataArr = [NSMutableArray array];
    [APPTools GETWithURL:newStr par:nil success:^(id responseObject) {
        
        self.dataArr = [modelOfMintHome modelHandlerWithArray:responseObject[@"data"][@"posts"]];
        
        [_tabelView1 reloadData];
        
        
    } filed:^(NSError *error) {
        
    } ];
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}

@end
